package com.ansion.commonchat.contact.ui;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ansion.adapter.TurboAdapter;
import com.ansion.basedb.dao.model.UserModel;
import com.ansion.baseui.base.TVPFragmentUIProxy;
import com.ansion.baseui.base.recycler.FrescoStateOnScrollSpeedListener;
import com.ansion.commonchat.R;
import com.ansion.commonchat.contact.adapter.ContactDataItem;
import com.ansion.commonchat.contact.adapter.ContactDividerItem;
import com.ansion.commonchat.contact.adapter.ContactHeaderItem;
import com.ansion.commonchat.contact.adapter.ContactOtherItem;
import com.ansion.commonchat.contact.adapter.ContactShareItem;
import com.ansion.commonchat.contact.bean.ContactBaseBean;
import com.ansion.commonchat.contact.bean.ContactHeaderBean;
import com.ansion.commonchat.contact.core.ContactDiffCallback;
import com.ansion.commonchat.contact.core.ContactsPresenter;
import com.ansion.commonchat.contact.listener.IOnItemClickListener;
import com.ansion.commonchat.view.AlphabetView;
import com.ansion.constant.BConstant;
import com.ansion.extension.DelayInitDispatcher;
import com.ansion.extension.SafelyLinearLayoutManager;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.permission.RealRxPermission;
import com.ansion.view.pin_view.PinnedHeaderItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * RecyclerView
 */
public class ContactsAdapterUIProxy extends TVPFragmentUIProxy<ContactsFragmentNew, ContactsPresenter> implements IOnItemClickListener {

    protected RecyclerView mRecyclerView;
    private TurboAdapter<ContactBaseBean> mAdapter;
    private final FrescoStateOnScrollSpeedListener frescoStateOnScrollListener = new FrescoStateOnScrollSpeedListener();
    private AlphabetView mAlphabetView;
    private TextView mIndexText;

    public ContactsAdapterUIProxy(ContactsFragmentNew contactsFragmentNew) {
        super(contactsFragmentNew);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    public void initView() {
        mRecyclerView = findViewById(R.id.contacts);
        mRecyclerView.addOnScrollListener(frescoStateOnScrollListener);
        ContactOtherItem contactOtherItem = new ContactOtherItem();
        contactOtherItem.addOnItemClickListener(this);
        ContactDataItem contactDataItem = new ContactDataItem();
        contactDataItem.addOnItemClickListener(this);

        mAdapter = TurboAdapter.with(Arrays.asList(contactOtherItem, contactDataItem,
                new ContactHeaderItem(),
                new ContactDividerItem(),
                new ContactShareItem()
        ));
        mRecyclerView.setLayoutManager(new SafelyLinearLayoutManager(getView().getContext()));
        mRecyclerView.addItemDecoration(new PinnedHeaderItemDecoration());
        mRecyclerView.setAdapter(mAdapter);

        mIndexText = findViewById(R.id.alpha_index_toast);
        mAlphabetView = findViewById(R.id.contacts_index_av);

        AlphabetViewListener listener = new AlphabetViewListener();
        mAlphabetView.setOnTouchingLetterChangedListener(listener);
    }

    public void checkKeys(List<String> sortKeys) {
        if (mAlphabetView != null) {
            mAlphabetView.checkKeys(sortKeys);
        }
    }

    public void refreshData(List<ContactBaseBean> contactList) {
        if (mAdapter != null) {
            if (mAdapter.getData() == null || mAdapter.getData().size() == 0) {
                mAdapter.setData(contactList).notifyDataSetChanged();
            } else {
                ContactDiffCallback contactDiffCallback = new ContactDiffCallback(mAdapter.getData(), contactList);
                mAdapter.updateByDiff(contactDiffCallback, contactList);
            }
        }
        DelayInitDispatcher.getInstance().start();
    }

    @Override
    public void onItemClick(UserModel userModel) {
        if (userModel == null) {
            return;
        }
//        if (ContactUtil.isServerId(userModel.getUserId())) {
//            ModuleApiFactory.getChatApi().openChatActivity(getView().getContext(), "" + userModel.getUserId());
//        } else {
//            ChatUtil.showUserInfo(getView().getContext(), userModel.getUserId());
//        }
    }

    @Override
    public void onRouteGroupChat() {
//        GroupListActivity.startActivity(getView().getContext());
    }

    @Override
    public void onRouteOAList() {
//        OfficialAccountListActivity.start(getView().getContext());
    }

    @Override
    public void removeAdsItem() {
        if (mAdapter != null && mAdapter.getData() != null) {
            List<ContactBaseBean> list = new ArrayList<>(mAdapter.getData());
            ContactBaseBean removeBean = null;
            for (ContactBaseBean bean : list) {
            }
            if (removeBean != null) {
                list.remove(removeBean);
                refreshData(list);
            }
        }
    }

    @Override
    protected void onDestroyFragment() {
        if (mRecyclerView != null) {
            mRecyclerView.removeOnScrollListener(frescoStateOnScrollListener);
        }
    }

    private class AlphabetViewListener implements
            AlphabetView.OnTouchingLetterChangedListener {
        @Override
        public void onTouchingLetterChanged(final String s) {

            if (s == null || mRecyclerView == null)
                return;

            if (TextUtils.isEmpty(s)) {
                if (mIndexText != null) {
                    mIndexText.setVisibility(View.GONE);
                }
            }

            int position = getAlphaIndex(s);

            if (position >= 0) {
                mIndexText.setText(s);
                mIndexText.setVisibility(View.VISIBLE);

                SafelyLinearLayoutManager manager = (SafelyLinearLayoutManager) mRecyclerView.getLayoutManager();
                if (manager != null) {
                    manager.scrollToPositionWithOffset(position, 0);
                }
            }
        }

    }

    private int getAlphaIndex(String key) {
        if (TextUtils.isEmpty(key)) {
            return -1;
        }
        ArrayList<ContactBaseBean> dataList = mAdapter.getData();

        for (ContactBaseBean contactBaseBean : dataList) {
            if (!(contactBaseBean instanceof ContactHeaderBean)) {
                continue;
            }
            if (key.equalsIgnoreCase(((ContactHeaderBean) contactBaseBean).cateTitle)) {
                return dataList.indexOf(contactBaseBean);
            }
        }
        return -1;
    }
}
